package com.yanchengtect.demo.progress.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONObject;
import com.yanchengtect.demo.progress.common.PageBean;
import com.yanchengtect.demo.progress.domain.TUser;

/**
* @author 文心雕龙 Email：xuwenlong@shike8888.com
* @version 创建时间：2018年9月4日 下午6:10:12
* 类说明:数据库操作类
*/
@Repository
public class TUserDao extends BaseDao<TUser> {

	@Autowired
	JdbcTemplate jdbcTemplate;
	
	/**-- 增 --*/
	public int add(TUser user) {
		 return addOne(user);
	}
	
	/**-- 删 --*/
	public int delete(int id) {
		
		String sql = "delete from t_user where id =?";
		return jdbcTemplate.update(sql,id);
	}
	
	/**-- 改 --*/
	public int update(TUser user) {
		
		String sql = "update t_user set name=? ,head_logo=? where id =?";
		return jdbcTemplate.update(sql,user.getName(),user.getHeadLogo(),user.getId());
		
	}
	
	/**-- 查 --*/
	
	public TUser findById(int id) {
		
		  return findById("t_user", id);
	}
	
	
	
	public void findPageByConditions(int type,PageBean<TUser> pageBean) {
		
		String sql_total = "select count(1) from t_user where type=?";
		
		String sql = "select name,create_time,head_logo from t_user where type=? limit ?,? ";
		
		Integer total = jdbcTemplate.queryForObject(sql_total,new Object[]{type}, Integer.class);
		
		List<TUser> list = jdbcTemplate.query(sql, new Object[]{type,pageBean.getStartNum(),pageBean.getPageSize()},new RowMapperUtil(TUser.class));
		
		pageBean.setTotalCount(total);
		pageBean.setPageData(list);
		
		System.out.println(JSONObject.toJSONString(pageBean));
	}
	
	
}
